﻿using DevExpress.Web.ASPxGridView;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDream.admin
{
    public partial class Staff_ : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dteFrom.Date = DateTime.Parse("01/01/2013");
                dteTo.Date = DateTime.Now;
            }
        }

        void Bindata()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            var list = from k in dx.UserDBs
                       where k.money > 0 && (k.ngaydangky >= dteFrom.Date && k.ngaydangky <= dteTo.Date)
                       select new
                       {
                           k.ID,
                           k.UserName,
                           k.FirtName,
                           k.Lastname,
                           k.phone,
                           k.ngaythamgia,
                           k.nguoigioithieu,
                           k.TuvanName,
                           k.gioitinh,
                           k.email,
                           k.kichhoat,
                           k.flag, 
                           k.money,
                           k.visible

                       };
            grvpResult.DataSource = list;
            grvpResult.DataBind();
        }

        protected void btnTim_Click(object sender, EventArgs e)
        {
            Bindata();
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                dx.sp_LockUser_ActiUser(txtAccout.Text.Trim(), false, false);
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
        }

        protected void btnKhoa_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                dx.sp_LockUser_ActiUser(txtAccout.Text.Trim(), false, true);
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
        }

        protected void grvpResult_Load(object sender, EventArgs e)
        {
            Bindata();
        }

        protected void grvpResult_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            try
            {
                if (e.RowType != GridViewRowType.Data) return;
                double price = Convert.ToDouble(e.GetValue("money"));
                
                if (price < 100)
                    e.Row.ForeColor = System.Drawing.Color.DarkRed;
                else if (price < 590)
                    e.Row.ForeColor = System.Drawing.Color.Blue;

                bool b = Convert.ToBoolean(e.GetValue("flag"));
            }
            catch (Exception) { }
        }

        protected void grvpResult_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e)
        {
            bool b = Convert.ToBoolean(e.GetValue("flag"));
            bool k = Convert.ToBoolean(e.GetValue("kichhoat"));
            if (e.DataColumn.FieldName == "UserName") 
                if(b==false)
                e.Cell.BackColor = System.Drawing.Color.LightCyan;
            if (e.DataColumn.FieldName == "Lastname")
                if (k == false)
                    e.Cell.BackColor = System.Drawing.Color.LightPink;
        }
    }
}